/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yy.bugzillahelp.tool;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @date 2011-10-9, 11:05:52
 *
 * @author hualun-alan <alan.zeng@lonnesol.com>
 */
public class SQLTool {

    private static final Logger log = Logger.getLogger(SQLTool.class.getName());

    public static void closeConnection(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException ex) {
                log.log(Level.SEVERE, "", ex);
            }
        }
    }

    public static void closeStatement(Statement statement) {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException ex) {
                log.log(Level.SEVERE, "", ex);
            }
        }
    }

    public static void closeResultSet(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException ex) {
                log.log(Level.SEVERE, "", ex);
            }
        }
    }

    public static void closeConnection(Connection conn, Statement stmt, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            log.log(Level.SEVERE, "", e);
        }
    }

    /**
     * excute update sql with database connection
     *
     * @param con a database connection
     * @param sql the sql will excute to update database
     * @return the number of effected rows. 0 means nothing happened. -1 means database action exception
     */
    public static int executeUpdate(Connection con, String sql) {
        if (con == null) {
            return -1;
        }

        Statement stmt = null;
        try {
            stmt = con.createStatement();
            return stmt.executeUpdate(sql);
        } catch (SQLException ex) {
            log.log(Level.INFO, "Error sql : {0}", sql);
            log.log(Level.SEVERE, "", ex);
        } finally {
            closeStatement(stmt);
        }
        return -1;
    }

    public static Statement executeQuery(Connection con, String sql) {
        if (con == null) {
            return null;
        }

        try {
            Statement stmt = con.createStatement();
            stmt.executeQuery(sql);
            return stmt;
        } catch (SQLException ex) {
            log.log(Level.INFO, "Error sql : {0}", sql);
            log.log(Level.SEVERE, "", ex);
        }
        return null;
    }

    public static boolean isActive(Connection con) {
        if (con == null) {
            return false;
        }

        try {
            if (con.isClosed()) {
                return false;
            }
        } catch (SQLException ex) {
            log.log(Level.SEVERE, "", ex);
            return false;
        }

        return true;
    }

    public static int countEffectRow(int... array) {
        int sum = 0;
        for (int row : array) {
            if (row == -1) {
                return -1;
            }
            sum += row;
        }
        return sum;
    }
}
